package web;

import java.util.List;
import java.util.Map;

import vo.Favorite;
import vo.User;
import business.FavoriteService;
import business.UserService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class FavoriteAction implements Preparable {
	private FavoriteService service;
	private List<Favorite> favorites;
	private UserService userService;
	private String id;
	private User curUser;

	//分页变量
	private int curPage;
	private int totalPage;
	private int pageSize = 5;

	public String listFavorite() {
		System.out.println("curPage:" + curPage);
		int totalCount = (int) service.getTotalCount();
		System.out.println("totalCount:" + totalCount);
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("totalCount / pageSize:" + totalCount / pageSize);

		if (curPage == 0)
			curPage = 1;
		this.favorites = service.getFavoritesByUserId((curPage - 1) * pageSize, pageSize, curUser.getId());
		System.out.println("favorites count:" + this.favorites.size());

		return Action.SUCCESS;
	}

	public String addFavorite() {

		System.out.println("add fv:" + id);
		System.out.println("curUserId:" + curUser.getId());
		Favorite tmp = service.getByUserIdItemId(curUser.getId(), id);
		System.out.println("tmp:" + tmp);
		if (tmp == null) {
			System.out.println("首次添加fav:" + id);
			Favorite fv = new Favorite();
			fv.setItemId(id);
			fv.setUser(curUser);
			service.addFavorite(fv);
		}
		return "favSucc";
	}

	public List<Favorite> getFavorites() {
		return favorites;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
	}

	public void setFavoriteService(FavoriteService service) {
		this.service = service;
	}

	public void setService(FavoriteService service) {
		this.service = service;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public FavoriteService getService() {
		return service;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setFavorites(List<Favorite> favorites) {
		this.favorites = favorites;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

}